package core;


import utils.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @create: 2019-04-26 15:29
 * @author: Nick.Ma
 * @Desc:
 */
public class Box extends Thread implements ActionListener, WindowListener {

    private static int totalTime = 40;
    private static float totalDiff = 0.0f;
    private TextField tf1, tf2, tf3, tf4, tfStart;
    private TextArea taLog;         //日志输出区域 不可以设置颜色相关
    private JButton jbtStart;       //开始按钮

    public void run() {
        //父窗体
        JFrame jFrame = new JFrame("青青河边草");
        // 创建按钮
        jbtStart = new JButton("开始");
        jbtStart.addActionListener(this);
        // 创建相关的文本域
        tf1 = new TextField("08:00");
        tf2 = new TextField("08:00");
        tf3 = new TextField("08:00");
        tf4 = new TextField("08:00");
        tfStart = new TextField("09:00");

        taLog = new TextArea();
        taLog.setColumns(30);
        taLog.setRows(150);
        taLog.setBackground(Color.CYAN);
        taLog.setFont(new Font("宋体", Font.BOLD, 16));
        taLog.setEditable(false);

        // 创建相关的Label标签
        JLabel label1 = new JLabel("时间一");
        JLabel label2 = new JLabel("时间二");
        JLabel label3 = new JLabel("时间三");
        JLabel label4 = new JLabel("时间四");
        JLabel labelStart = new JLabel("开始时间");
        JLabel labelLog = new JLabel("输出日志信息");
        JLabel labelTip1 = new JLabel("<html>小马哥,特别制作:<br>1.按照格式填写时间<br>2.尽信工具不如无工具<br>3.总工时默认40H/W</html>");
        JLabel labelTip2 = new JLabel("<html>小马哥,简单说明:<br>1.时间1,2,3,4填写当天的实际工时<br>2.开始时间,目标天的上班时间<br>3.输出日志:EndTime2即目标天的下班时间</html>");

        //四个时间
        JPanel panel1 = new JPanel(new GridLayout(2, 4));
        panel1.add(label1);
        panel1.add(label2);
        panel1.add(label3);
        panel1.add(label4);
        panel1.add(tf1);
        panel1.add(tf2);
        panel1.add(tf3);
        panel1.add(tf4);

        //开始时间
        JPanel panel2 = new JPanel(new GridLayout(2, 1));
        panel2.add(labelStart, BorderLayout.CENTER);
        panel2.add(tfStart, BorderLayout.CENTER);
        //日志输出
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        panel3.add(labelLog);
        panel3.add(taLog);

        //开始按钮及左右信息
        JPanel panel4 = new JPanel(new GridLayout(1, 3));
        panel4.add(labelTip1);
        panel4.add(jbtStart);
        panel4.add(labelTip2);

        //上级布局
        JPanel panel22 = new JPanel(new GridLayout(4, 1));
        panel22.add(panel1);
        panel22.add(panel2);
        panel22.add(panel3);
        panel22.add(panel4);

        jFrame.setLayout(new BorderLayout());
        jFrame.add(panel22, BorderLayout.CENTER);

        // 初始化JFrame窗口
        jFrame.setLocation(800, 300);
        jFrame.setSize(600, 500);
        jFrame.setBackground(Color.darkGray);
        jFrame.setResizable(true);
        jFrame.setVisible(true);

    }

    public void actionPerformed(ActionEvent view) {
        if (view.getSource() == jbtStart) {
            String day1 = tf1.getText();
            String day2 = tf2.getText();
            String day3 = tf3.getText();
            String day4 = tf4.getText();
            String startTime = tfStart.getText();

            if (StringUtils.isNotEmpty(day1) && StringUtils.isNotEmpty(day2) && StringUtils.isNotEmpty(day3) && StringUtils.isNotEmpty(day4) && StringUtils.isNotEmpty(startTime)) {
                calcTime(day1, day2, day3, day4);
                whatTime(startTime);
            }
        }
    }

    private void calcTime(String day1, String day2, String day3, String day4) {
        String day5 = "";
        int h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0;
        if (StringUtils.isNotEmpty(day1)) {
            String[] splitTime1 = day1.split(":");
            h1 = Integer.parseInt(splitTime1[0]);
            m1 = Integer.parseInt(splitTime1[1]);
        }
        if (StringUtils.isNotEmpty(day2)) {
            String[] splitTime1 = day2.split(":");
            h2 = Integer.parseInt(splitTime1[0]);
            m2 = Integer.parseInt(splitTime1[1]);
        }
        if (StringUtils.isNotEmpty(day3)) {
            String[] splitTime1 = day3.split(":");
            h3 = Integer.parseInt(splitTime1[0]);
            m3 = Integer.parseInt(splitTime1[1]);
        }
        if (StringUtils.isNotEmpty(day4)) {
            String[] splitTime1 = day4.split(":");
            h4 = Integer.parseInt(splitTime1[0]);
            m4 = Integer.parseInt(splitTime1[1]);
        }
        if (StringUtils.isNotEmpty(day5)) {
            String[] splitTime1 = day5.split(":");
            h5 = Integer.parseInt(splitTime1[0]);
            m5 = Integer.parseInt(splitTime1[1]);
        }
        int totalH = h1 + h2 + h3 + h4 + h5;
        int totalM = m1 + m2 + m3 + m4 + m5;
        float totalSum = totalH + totalM / 60f;
        totalDiff = totalTime - totalSum;
        taLog.append("totalSum:" + totalSum);
        taLog.append("\n");
        taLog.append("totalDif:" + totalDiff);
        taLog.append("\n");
    }

    private void whatTime(String startTime) {
        int startH = 0;
        int startM = 0;
        if (StringUtils.isNotEmpty(startTime)) {
            String[] splitTime1 = startTime.split(":");
            startH = Integer.parseInt(splitTime1[0]);
            startM = Integer.parseInt(splitTime1[1]);
        }
        float resM = totalDiff % 1;
        int resH = (int) (totalDiff / 1);
        float v = resM * 60 + startM;
        if (v >= 60) {
            v = v - 60;
            startH++;
        }
        int endH = startH + resH + 1;
        float endM = v;
        taLog.append("xx:" + resM + "hh:" + resH);
        taLog.append("\n");
        taLog.append("endH:" + endH + "endM:" + endM);
        taLog.append("\n");
        taLog.append("EndTime:" + endH + ":" + endM);
        taLog.append("\n");
        int ceilH = (int) Math.ceil(endM);
        String endTime = String.format("%02d", ceilH);
        taLog.append("EndTime2:" + endH + ":" + endTime);
        taLog.append("\n");
    }


    public void windowOpened(WindowEvent e) {

    }

    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }

    public void windowIconified(WindowEvent e) {

    }

    public void windowDeiconified(WindowEvent e) {

    }

    public void windowActivated(WindowEvent e) {

    }

    public void windowDeactivated(WindowEvent e) {

    }
}